#ifndef VIEWSUBJECT_H
#define VIEWSUBJECT_H

#include <list>
#include "observer.h"

/**
 * @brief The ViewSubject class
 * View充当被观察者时，继承该类
 */
class ViewSubject
{
public:
    /**
     * @brief 添加一个observer
     * @param observerPtr
     */
    virtual void addObserver(Observer* observerPtr){
        if(std::find(std::begin(m_observerList),std::end(m_observerList),observerPtr) == std::end(m_observerList)){
            m_observerList.push_back(observerPtr);
        }
    }

    /**
     * @brief 移除一个observer
     * @param observerPtr
     */
    virtual void removeObserver(Observer* observerPtr){
        m_observerList.remove(observerPtr);
    }

    /**
     * @brief 通知m_observerList中的所有observer，view发生了改变
     * @param dataPtr
     */
    virtual void notify(QSharedPointer<Data> dataPtr){
        for(auto observer:m_observerList){
            if(observer!=nullptr){
                observer->update(dataPtr);
            }
        }
    }

protected:
    std::list<Observer*> m_observerList;
};

#endif // VIEWSUBJECT_H
